package test;

/**
 * @author 豆包LU
 * @date 2023/4/19 9:59
 */
public class WaitingState {

    public static void main(String[] args) {
        WaitingState waitingState1 = new WaitingState();
        WaitingState waitingState2 = new WaitingState();

        Thread thread1 = new Thread(() -> {
            synchronized (WaitingState.class) {
                try {
                    System.out.println("Thread 1 acquired lock");
                    WaitingState.class.wait(); // 线程1等待，并释放锁
                    System.out.println("Thread 1 released lock");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (WaitingState.class) {
                try {
                    System.out.println("Thread 2 acquired lock");
                    WaitingState.class.wait(); // 线程2等待，并释放锁
                    System.out.println("Thread 2 released lock");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread1.start();
        thread2.start();

        while (true) {
            Thread.State state1 = thread1.getState();
            Thread.State state2 = thread2.getState();
            System.out.println("Thread 1 state: " + state1);
            System.out.println("Thread 2 state: " + state2);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
